package com.example.common.tools

import android.os.Handler
import android.os.Looper
import androidx.annotation.NonNull
import java.util.concurrent.Executor
import java.util.concurrent.Executors

class AppExecutors {

    private var mDiskIO: Executor? = null

    private var mNetworkIO: Executor? = null

    private var mMainThread: Executor? = null


    constructor(){
        mDiskIO = Executors.newSingleThreadExecutor()
        mNetworkIO = Executors.newFixedThreadPool(3)
        mMainThread = MainThreadExecutor()
    }


    fun diskIO(): Executor? {
        return mDiskIO
    }

    fun networkIO(): Executor? {
        return mNetworkIO
    }

    fun mainThread(): Executor? {
        return mMainThread
    }

    private class MainThreadExecutor : Executor {
        private val mainThreadHandler =
            Handler(Looper.getMainLooper())

        override fun execute(@NonNull command: Runnable) {
            mainThreadHandler.post(command)
        }
    }
}

